#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 3e4 + 5;

int n, m;
int f[N], d[N], sz[N];
void init() { rep(i, 1, 30000) f[i] = i, d[i] = 0, sz[i] = 1; }
int find(int x) {
  if (f[x] == x) return x;
  int y = f[x];
  f[x] = find(f[x]);
  d[x] += d[y];
  return f[x];
}
void merge(int x, int y) {
  int fx = find(x), fy = find(y);
  if (fx == fy) return;
  f[fx] = fy;
  d[fx] = sz[fy];
  sz[fy] += sz[fx];
}
int dis(int x, int y) {
  int fx = find(x), fy = find(y);
  if (fx != fy) return -1;
  return abs(d[x] - d[y]) - 1;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  init();
  int t;
  cin >> t;
  while (t--) {
    char op;
    int u, v;
    cin >> op >> u >> v;
    if (op == 'M')
      merge(u, v);
    else
      cout << dis(u, v) << endl;
  }
  return 0;
}